package com.yopuo.web.controller.oss;

import com.google.gson.Gson;
import com.yopuo.common.constant.OssConstants;
import com.yopuo.common.core.controller.BaseController;
import com.yopuo.common.core.domain.AjaxResult;
import com.yopuo.common.core.page.TableDataInfo;
import com.yopuo.common.exception.oss.OssException;
import com.yopuo.common.utils.ParamsUtils;
import com.yopuo.common.utils.ValidatorUtils;
import com.yopuo.system.domain.SysConfig;
import com.yopuo.system.domain.SysOss;
import com.yopuo.system.service.ISysConfigService;
import com.yopuo.system.service.ISysOssService;
import com.yopuo.web.config.oss.CloudStorageConfig;
import com.yopuo.web.config.oss.OSSFactory;
import com.yopuo.web.config.oss.group.AliyunGroup;
import com.yopuo.web.config.oss.group.QcloudGroup;
import com.yopuo.web.config.oss.group.QiniuGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 文件上传
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("sys/oss")
public class SysOssController extends BaseController {
	@Autowired
	private ISysOssService sysOssService;
    @Autowired
    private ISysConfigService sysConfigService;

    private final static String KEY = OssConstants.CLOUD_STORAGE_CONFIG_KEY;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@PreAuthorize("@ss.hasPermi('system:oss:list')")
	public TableDataInfo list(@RequestParam Map<String, Object> params){
		startPage();
		List<SysOss> ossList = sysOssService.list();
		return getDataTable(ossList);
	}


    /**
     * 云存储配置信息
     */
    @RequestMapping("/config")
	@PreAuthorize("@ss.hasPermi('system:oss:config')")
    public AjaxResult config(){
        CloudStorageConfig config = sysConfigService.getConfigObject(KEY, CloudStorageConfig.class);
        return AjaxResult.success(config);
    }


	/**
	 * 保存云存储配置信息
	 */
	@RequestMapping("/saveConfig")
	@PreAuthorize("@ss.hasPermi('system:oss:saveConfig')")
	public ParamsUtils saveConfig(@RequestBody CloudStorageConfig config){
		//校验类型
		ValidatorUtils.validateEntity(config);

		if(config.getType() == OssConstants.CloudService.QINIU.getValue()){
			//校验七牛数据
			ValidatorUtils.validateEntity(config, QiniuGroup.class);
		}else if(config.getType() == OssConstants.CloudService.ALIYUN.getValue()){
			//校验阿里云数据
			ValidatorUtils.validateEntity(config, AliyunGroup.class);
		}else if(config.getType() == OssConstants.CloudService.QCLOUD.getValue()){
			//校验腾讯云数据
			ValidatorUtils.validateEntity(config, QcloudGroup.class);
		}

		SysConfig sysConfig = new SysConfig();
		sysConfig.setConfigKey(KEY);
		sysConfig.setConfigValue(new Gson().toJson(config));
		sysConfigService.updateConfig(sysConfig);

		return ParamsUtils.ok();
	}
	

	/**
	 * 上传文件
	 */
	@RequestMapping("/upload")
	@PreAuthorize("@ss.hasPermi('system:oss:upload')")
	public ParamsUtils upload(@RequestParam("file") MultipartFile file) throws Exception {
		if (file.isEmpty()) {
			throw new OssException("上传文件不能为空");
		}

		//上传文件
		String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
		String url = OSSFactory.build(1).uploadSuffix(file.getBytes(), suffix);

		//保存文件信息
		SysOss ossEntity = new SysOss();
		ossEntity.setUrl(url);
		ossEntity.setCreateTime(new Date());
		sysOssService.save(ossEntity);

		return ParamsUtils.ok().put("url", url);
	}


	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@PreAuthorize("@ss.hasPermi('system:oss:delete')")
	public ParamsUtils delete(@RequestBody Long[] ids){
		sysOssService.removeByIds(Arrays.asList(ids));

		return ParamsUtils.ok();
	}

}
